package com.zhentao.user;

import java.util.Random;

/**
 * @ClassName: RandomNicknameGenerator
 * @Author: 振涛教育_Q
 * @Date: 2024/12/18 18:22
 */
public class RandomNicknameGenerator {
    // 常用汉字的Unicode编码范围（部分范围示例，可按需扩充）
    private static final char[] CHINESE_CHARACTERS = new char[20902];
    static {
        for (int i = 0; i < 20902; i++) {
            CHINESE_CHARACTERS[i] = (char) (0x4E00 + i);
        }
    }

    // 数字字符集
    private static final char[] DIGITS = "0123456789".toCharArray();
    // 英文字母字符集（小写）
    private static final char[] LETTERS = "abcdefghijklmnopqrstuvwxyz".toCharArray();


    public static String generateNickname() {
        Random random = new Random();
        // 设定昵称长度范围，这里是3到8个字符，可调整
        int length = random.nextInt(6) + 3;
        StringBuilder nickname = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int choice = random.nextInt(3);
            switch (choice) {
                case 0:
                    // 选择汉字
                    nickname.append(CHINESE_CHARACTERS[random.nextInt(CHINESE_CHARACTERS.length)]);
                    break;
                case 1:
                    // 选择数字
                    nickname.append(DIGITS[random.nextInt(DIGITS.length)]);
                    break;
                case 2:
                    // 选择字母
                    nickname.append(LETTERS[random.nextInt(LETTERS.length)]);
                    break;
            }
        }
        return nickname.toString();
    }
}
